(:
   Conversion of a display file to Excel XML format
 :)
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
declare namespace ss = "http://example.org";

let $il := doc($arguments[1])//item
    
return
<Workbook
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:html="http://www.w3.org/TR/REC-html40"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
>
<Worksheet ss:Name="DREW">
<Table ss:ExpandedColumnCount="6" ss:ExpandedRowCount="{count($il)}" 
   x:FullColumns="1" x:FullRows="1"
  >
   <Column ss:Index="2" ss:AutoFitWidth="0" ss:Width="150.0"/>
   <Column ss:Index="5" ss:AutoFitWidth="0" ss:Width="250.0"/>
   <Column ss:Index="6" ss:AutoFitWidth="0" ss:Width="150.0"/>
   {
   for $e in $il
   let $tim := ($e/info[@name="time"]/time/date[1],$e/info[@name="Time"]/time/date[1]) 
   let $ty := string(($e/info[@name="type"], $e/info[@name="Tool"]))
   let $us := ($e/info[@name="user"], $e/info[@name="User"])
   let $tx := ($e/info[@name="Message"], $e/info[@name="content"])
   let $cat := ($e/info[@name="Category"])
   return
      (<Row>
        <Cell><Data ss:Type="String">{string($e/info[@name="N"])}</Data></Cell>
        <Cell><Data ss:Type="String">{jj:dateFromDREW($tim)}</Data></Cell>
        <Cell><Data ss:Type="String">{$ty}</Data></Cell>
        <Cell><Data ss:Type="String">{string($us)}</Data></Cell>
        <Cell>
           {if ($ty = "threaded-chat")
            then
                attribute { "ss:StyleID" } { "s23" }
            else
            if ($ty  = "chat")
            then
                attribute { "ss:StyleID" } { "s24" }
            else
            if ($ty  = "event")
            then
                attribute { "ss:StyleID" } { "s25" }
            else
            if ($ty  = "transcription")
            then
                attribute { "ss:StyleID" } { "s26" }
            else
                ()
         ,<Data ss:Type="String">{
             string($tx/text()), string($tx/text/text())
         }</Data>}</Cell>
         <Cell>{
              attribute { "ss:StyleID" } { 
                  let $c := fn:lower-case(string($cat/text/@bgcolor))
                  return
                  if ($c = "white") then  "cwhi"
                  else if ($c = "yellow") then "cyel"
                  else if ($c = "orange") then  "corg"
                  else if ($c = "red") then  "cred"
                  else if ($c = "green") then  "cgre"
                  else if ($c = "blue") then  "cblu"
                  else if ($c = "darkblue") then  "cdbl"
                  else if ($c = "purple") then  "cpur"
                  else if ($c = "black") then  "cbla"
                  else     "cwhi"  },   
            <Data ss:Type="String">{
            $cat/text/text()
         }</Data>}</Cell>
      </Row>)
   }
</Table>
</Worksheet>
</Workbook>
